package com.agent.membership.controller.app;

import com.agent.common.entity.UserContext;
import com.agent.common.result.ApiResponse;
import com.agent.common.utils.UserInfoUtils;
import com.agent.membership.dto.UpgradeRequest;
import com.agent.membership.dto.UpgradeResult;
import com.agent.membership.entity.MembershipPlan;
import com.agent.membership.entity.MembershipSubscription;
import com.agent.membership.service.CreditService;
import com.agent.membership.service.MembershipService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员控制器
 * @author Jeff_Wan
 * @description 提供会员相关的API接口
 */
@RestController
@RequestMapping("/membership")
@RequiredArgsConstructor
public class MembershipController {
    private final MembershipService membershipService;
    private final CreditService creditService;

    /**
     * 获取当前会员信息
     *
     * @return 会员信息
     */
    @GetMapping("/current")
    public ApiResponse<MembershipSubscription> getCurrentMembership() {
        MembershipSubscription subscription =
                membershipService.getUserMembership(UserInfoUtils.getUserId());
        return ApiResponse.success(subscription);
    }

    /**
     * 创建新订阅
     *
     * @param request 升级请求
     * @return 创建的订阅信息
     */
    @PostMapping("/subscribe")
    public ApiResponse<MembershipSubscription> createSubscription(@RequestBody UpgradeRequest request) {
        MembershipSubscription subscription = membershipService.createSubscription(
                UserInfoUtils.getUserId(), request.getNewPlanType(), request.getNewSubscriptionType(), Boolean.TRUE);
        return ApiResponse.success(subscription);
    }

    /**
     * 升级会员计划
     *
     * @param request 升级请求
     * @return 升级后的会员信息
     */
    @PostMapping("/upgrade")
    public ApiResponse<MembershipSubscription> upgradeMembership(@RequestBody UpgradeRequest request) {
        MembershipSubscription subscription = membershipService.upgradeMembership(
                UserInfoUtils.getUserId(), request.getNewPlanType(), request.getNewSubscriptionType());
        return ApiResponse.success(subscription);
    }

    /**
     * 取消自动续费
     *
     * @return 操作结果
     */
    @PostMapping("/cancel/auto/renew")
    public ApiResponse<String> cancelAutoRenew() {
        return ApiResponse.judge(membershipService.cancelAutoRenew(UserInfoUtils.getUserId()));
    }

    // -----------------------------------------------------

    /**
     * 获取用户会员订阅信息
     *
     * @return 会员信息
     */
    @GetMapping("/rss/info")
    public ApiResponse<MembershipSubscription> getUserMembership() {
        return ApiResponse.success(membershipService.getUserMembershipCredit(UserInfoUtils.getUserId()));
    }

    /**
     * 获取所有会员套餐配置
     * @return 套餐列表
     */
    @GetMapping("/plans")
    public ApiResponse<List<MembershipPlan>> getAllMembershipPlans() {
        return ApiResponse.success(membershipService.getAllMembershipPlansCredit());
    }

    /**
     * 计算升级费用
     *
     * @param request 升级请求
     * @return 升级费用
     */
    @PostMapping("/upgrade/cost")
    public ApiResponse<BigDecimal> calculateUpgradeCost(@RequestBody UpgradeRequest request) {
        return ApiResponse.success(creditService.calculateUpgradeCost(
                UserInfoUtils.getUserId(), request.getNewPlanType(), request.getNewSubscriptionType()));
    }
}